package ch.ethz.fcl.listener;

import java.util.ArrayList;

import com.google.android.maps.MapView;

import ch.ethz.fcl.overlays.OverlayManager;
import ch.ethz.fcl.udp.Protocal;
import ch.ethz.fcl.udp.UDPClient;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class ModeButtonClickListener implements OnClickListener {
	private final String TAG = "ButtonClickListener";
	private static ArrayList<ImageButton> buttons;
	private MapView mapView = null;
	private String msg = "MetroBuzz:Mode:";
	private UDPClient client;

	public ModeButtonClickListener(ImageButton button, MapView mapView2) {
		Log.d(TAG, "ButtonClickListener constructor");
		if (this.mapView == null)
			this.mapView = (MapView) mapView2;
		if (buttons == null || buttons.size() == 4)
			buttons = new ArrayList<ImageButton>();
		buttons.add(button);
		client = new UDPClient();
	}

	@Override
	public void onClick(View view) {
		mapView.getOverlays().clear();
		for (int i = 0; i < buttons.size(); i++) {
			if (view.getTag().equals(buttons.get(i).getTag())) {
				if (view.getTag().equals("twoline")
						|| view.getTag().equals("twocircle"))
					mapView.getOverlays().add(OverlayManager.getOverlay(i - 1));
				mapView.getOverlays().add(OverlayManager.getOverlay(i));
				buttons.get(i).setBackgroundColor(Color.GREEN);
				msg = "MetroBuzz:Mode:";
				switch(i){
				case 0:
					msg += Protocal.MODE_ONE_CIRCLE;
					OverlayManager.getCircleOverlay(0).sendInfo();
					break;
				case 1:
					msg += Protocal.MODE_TWO_CIRCLE;
					OverlayManager.getCircleOverlay(0).sendInfo();
					OverlayManager.getCircleOverlay(1).sendInfo();
					break;
				case 2:
					msg += Protocal.MODE_ONE_LINE;
					OverlayManager.getScreenLineOverlay(2).sendInfo();
					break;
				case 3:
					msg += Protocal.MODE_TWO_LINE;
					OverlayManager.getScreenLineOverlay(2).sendInfo();
					OverlayManager.getScreenLineOverlay(3).sendInfo();
					break;
				}
				client.setMessage(msg);
				client.run();
			} else {
				buttons.get(i).setBackgroundColor(Color.GRAY);
			}
		}
		mapView.invalidate();
	}
}
